#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct edge {
    int to;
    edge* next;
    edge() : to(-1), next(nullptr) {}
    edge(int to) : to(to), next(nullptr) {}
    edge(int to, edge* next) : to(to), next(next) {}
};

int indegree[2004];
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
    edge v[numCourses];
    edge* tail[numCourses];
    for (int i = 0; i < numCourses; i++) tail[i] = &v[i];
    for (int i = 0; i < prerequisites.size(); i++) {
        int from = prerequisites[i][1];
        int to = prerequisites[i][0];
        tail[from]->next = new edge(to);
        tail[from] = tail[from]->next;
        indegree[to]++;
    }

    vector<int> ans;
    queue<int> Q;
    for (int i = 0; i < numCourses; i++) {
        if (indegree[i] == 0) {
            ans.push_back(i);
            Q.push(i);
        }
    }
    
    while (!Q.empty()) {
        edge* p = &v[Q.front()];
        while (p->next) {
            p = p->next;
            --indegree[p->to];
            if (indegree[p->to] == 0) {
                ans.push_back(p->to);
                Q.push(p->to);
            }
        }
        Q.pop();
    }

    if (ans.size() == numCourses) return ans;
    return vector<int>();
}

int main() {
    vector<vector<int>> pre = {{1, 0}};
    vector<int> ans = findOrder(2, pre);
    for (int i = 0; i < ans.size(); i++) cout << ans[i] << ", ";
    return 0;
}